package com.sales.service.queue;

import java.util.concurrent.LinkedBlockingQueue;

import com.sales.service.http.ServiceRequest;

import android.util.Log;

/**
 * This class - ServiceQueueManager is use to manage the queue used.
 * This class is also instantiated by Singleton Design Pattern.
 * The constructor is made private.
 * The instance of the class is of Private static modifier.
 * A public method getInstance() to get the instance of the class is declared.
 * And this method is made as synchronised for thread safe.
 * Hence at any time the instance of this class is only one.
 * 
 * This class handles the queue by defining its properties in methods.
 * 
 * @author Sega
 *
 */
public class ServiceQueueManager {
	
	/**
	 * Strings for debugging process by log.i method
	 */
	private static final String TagProject = "SalesService";
	private static final String TagPage = "QueueManager ::";

	/**
	 * private static instance for Singleton class.
	 */
	private static ServiceQueueManager instance=null;
	
	/**
	 * LinkedBlockQueue is declared as private.
	 */
	private LinkedBlockingQueue<ServiceRequest> stringQueue=null;
	
	/**
	 * private constructor for singleton design.
	 */
	private ServiceQueueManager(){
		stringQueue = new LinkedBlockingQueue<ServiceRequest>();
	}
	
	/**
	 * getInstance() method for returning the instance of the class.
	 * If the instance is null then the instance is instantiated or else it 
	 * returns the static instance that is already available. 
	 * @return the instance of the class.
	 */
	public static synchronized ServiceQueueManager getInstance(){
		if (instance==null){
			instance = new ServiceQueueManager();
		}
		return instance;
	}
	
	/**
	 * This method is used to add elements in the queue.
	 * the queue's put() method is used.
	 * 
	 * @param value is of type String. 
	 * It is the String Element that has to be added in the Queue.
	 */
	public void queuePut(ServiceRequest value){
		try {
			stringQueue.put(value);
		} catch (InterruptedException e) {
			Log.i(TagProject, TagPage + " queuePut() Error : " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
			e.printStackTrace();
		}
	}
	
	/**
	 * This method is used to retrieve and read the head of the queue.
	 * It returns the head queue element.
	 */
	public ServiceRequest queuePoll(){
		return stringQueue.poll();
	}
	
	/**
	 * This method removes the desired element from the queue.
	 * @param value is the desired element that needs to be removed.
	 */
	public void queueRemove(String value){
		stringQueue.remove(value);
	}
	
	/**
	 * This method removes all the elements in the queue by its poll() method.
	 */
	public void queueRemoveAll(){
		ServiceRequest pollResult = stringQueue.poll();
		while (pollResult!=null){
			pollResult = stringQueue.poll();
		}
	}
	
	/**
	 * This is same as the above Remove method, because it calls that method.
	 */
	public void queueShutdown(){
		queueRemoveAll();
		stringQueue= null;
	}
	
	/**
	 * This method is used to get the size of the queue, ie the total no of elements in the queue.
	 * @return the count of elements is returned as int type.
	 */
	public int queueSize(){
		return stringQueue.size();
	}
	
}
